//
//  StoreListSearchCell.m
//  KYRR
//
//  Created by kuyuZJ on 2017/4/19.
//
//

#import "StoreListSearchCell.h"


@interface StoreListSearchCell ()

@property(nonatomic,strong) UIImageView* storeLogo;
@property(nonatomic,strong) UILabel* labelStoreName;
@property(nonatomic,strong) UILabel* labelSend;
@property(nonatomic,strong) UIImageView* arrow;
@property(nonatomic,strong) UILabel* labelLine;

@property(nonatomic,strong) MStore* entity;
@property(nonatomic,strong) NSString* keyword;

@end

@implementation StoreListSearchCell{
    CGSize arrowSize,logoSize;
    CGFloat lineHeight;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        self.contentView.backgroundColor = [UIColor whiteColor];
        arrowSize = CGSizeMake(6.f, 10.f);
        logoSize = CGSizeMake(40.f, 40.f);
        lineHeight = 0.5f;
        [self.contentView addSubview:self.storeLogo];
        [self.contentView addSubview:self.labelStoreName];
        [self.contentView addSubview:self.labelSend];
        [self.contentView addSubview:self.arrow];
        [self.contentView addSubview:self.labelLine];
        
        NSArray<NSString*>* formats = @[@"H:|-leftEdge-[storeLogo(==logoWidth)]-leftEdge-[labelStoreName]-leftEdge-[labelSend(==70)]-5-[arrow(==arrowWidth)]-leftEdge-|",
                                         @"V:[storeLogo(==logoHeight)]", @"V:|-defEdge-[labelStoreName]-defEdge-|", @"V:|-defEdge-[labelSend]-defEdge-|", @"V:[arrow(==arrowHeight)]",
                                        @"H:[storeLogo]-leftEdge-[labelLine]-defEdge-|", @"V:[labelLine(==lineHeight)]-defEdge-|"];
        NSDictionary<NSString*,id>* metrics = @{ @"defEdge":@(0), @"leftEdge":@(10), @"topEdge":@(10),
                                                  @"logoWidth":@(logoSize.width), @"logoHeight":@(logoSize.height),
                                                  @"arrowWidth":@(arrowSize.width), @"arrowHeight":@(arrowSize.height),
                                                  @"lineHeight":@(lineHeight)};
        NSDictionary<NSString*,id>* views = @{ @"storeLogo":self.storeLogo, @"labelStoreName":self.labelStoreName, @"labelSend":self.labelSend, @"arrow":self.arrow,
                                                @"labelLine":self.labelLine};
        [formats enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSLog( @"%@ %@",[self class],obj);
            NSArray<NSLayoutConstraint*>* constraints = [NSLayoutConstraint constraintsWithVisualFormat:obj options:0 metrics:metrics views:views];
            [self.contentView addConstraints:constraints];
        }];
        
        [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.storeLogo attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.f]];
        [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.arrow attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.f]];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)loadData:(MStore *)entity keyword:(NSString *)keyword{
    if(entity){
        _entity = entity;
        _keyword = keyword;
        [self.storeLogo sd_setImageWithURL:[NSURL URLWithString:self.entity.logo] placeholderImage:[UIImage imageNamed:kDefaultImage]];
        NSMutableAttributedString* attributeStr = [[NSMutableAttributedString alloc]initWithString:self.entity.storeName];
        [attributeStr addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} range:[self.entity.storeName rangeOfString:self.keyword]];
        self.labelStoreName.attributedText = attributeStr;
        self.labelSend.text = [NSString stringWithFormat: @"￥%@起送",self.entity.freeShip];
    }
}

#pragma mark =====================================================  property pacakge
-(UIImageView *)storeLogo{
    if(!_storeLogo){
        UIImageView* empty = [[UIImageView alloc]init];
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _storeLogo = empty;
    }
    return _storeLogo;
}

-(UILabel *)labelStoreName{
    if(!_labelStoreName){
        UILabel* empty = [[UILabel alloc]init];
        empty.font = [UIFont systemFontOfSize:15.f];
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _labelStoreName = empty;
    }
    return _labelStoreName;
}

-(UILabel *)labelSend{
    if(!_labelSend){
        UILabel* empty = [[UILabel alloc]init];
        empty.font = [UIFont systemFontOfSize:12.f];
        empty.textColor = theme_Fourm_color;
        empty.textAlignment = NSTextAlignmentRight;
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _labelSend = empty;
    }
    return _labelSend;
}

-(UIImageView *)arrow{
    if(!_arrow){
        UIImageView* empty = [[UIImageView alloc]init];
        [empty setImage:[UIImage imageNamed: @"icon-arrow-right"]];
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _arrow = empty;
    }
    return _arrow;
}

-(UILabel *)labelLine{
    if(!_labelLine){
        UILabel* empty = [[UILabel alloc]init];
        empty.backgroundColor = theme_line_color;
        empty.translatesAutoresizingMaskIntoConstraints = NO;
        _labelLine = empty;
    }
    return _labelLine;
}









@end
